﻿@page "/admin/listmessages"
@attribute [Authorize(Roles = "Admin")]

@inject IHttpService _httpService
@inject IPopupService PopupService
@inject NavigationManager NavigationManager

<MDCard>
    <DataTableCard TModel="MessageOverviewModel" Headers="_headers" ApiUrl="@(_baseUrl)" ShowAddButton="false" Title="@($"{_name}列表")" @ref="dataTableCard">
        <ItemColContent>
            @if (context.Header.Value == "actions")
            {
                <MIcon Color="info"
                       Class="mr-2"
                       OnClick="()=>LookItem(context.Item)">@IconType.Look.ToIconString()</MIcon>
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.IsReaded))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.Admin.Users.Messages.StateChip IsReaded="@context.Item.IsReaded" />
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.UserId))
            {
                <CnGalWebSite.Shared.MasaComponent.PC.EditRecords.UserCenter.ReviewEditRecords.UserChip Id="@context.Item.UserId" Name="@context.Item.UserName" />
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.PostTime))
            {
                @context.Item.PostTime.ToString("yyyy-MM-dd HH:mm")
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.Type))
            {
                @context.Item.Type.GetDisplayName()
            }
            else if (context.Header.Value == nameof(MessageOverviewModel.Text))
            {
                <div style="white-space: pre-line;width: 300px;word-break: break-all;">
                    @context.Item.Text
                </div>
            }
            else
            {
                @context.Value
            }
        </ItemColContent>
    </DataTableCard>

</MDCard>



@code {
    private bool _editDialog;
    private long _editedItemId;
    private bool _detailDialog;

    private string _baseUrl = "api/space/ListMessages";
    private string _name = "消息";

    private List<DataTableHeader<MessageOverviewModel>> _headers = new List<DataTableHeader<MessageOverviewModel>>
    {
        new ()
          {
            Text= "Id",
            Align= DataTableHeaderAlign.Start,
            Value= nameof(FavoriteFolderOverviewModel.Id)
          },
          new (){ Text= "类型", Value= nameof(MessageOverviewModel.Type)},
          new (){ Text= "发送时间", Value= nameof(MessageOverviewModel.PostTime)},
          new (){ Text= "标题", Value= nameof(MessageOverviewModel.Title)},
          new (){ Text= "内容", Value= nameof(MessageOverviewModel.Text)},
          new (){ Text= "目标", Value= nameof(MessageOverviewModel.UserId)},
          new (){ Text= "是否已读", Value= nameof(MessageOverviewModel.IsReaded)},
          new (){ Text= "操作", Value= "actions",Sortable=false }
    };

    DataTableCard<MessageOverviewModel> dataTableCard;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    public void LookItem(MessageOverviewModel model)
    {
        NavigationManager.NavigateTo(model.Link);
    }
}
